import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

class Solution {
    public static List<Integer> findSubstring(String s, String[] words) {
        List<Integer> ret = new ArrayList<>();
        HashMap<String,Integer> hash1 = new HashMap<>();//保存字典中的数据
        for(String ss : words) hash1.put(ss,hash1.getOrDefault(ss,0) + 1);
        int len = words[0].length(),m = words.length;
        for(int i = 0; i < len; i++) {
            HashMap<String,Integer> hash2 = new HashMap<>();
            for(int count = 0, left = i, right = i; right + len <= s.length(); right += len) {
                //进窗口 + 维护
                String in = s.substring(right, right + len);
                hash2.put(in,hash2.getOrDefault(in,0) + 1);
                if(hash2.get(in) <= hash1.getOrDefault(in,0)) count++;
                //判断
                //出窗口 + 维护
                if(right - left + 1 > m * len) {
                    String out = s.substring(left,left + len);
                    if(hash2.get(out) <= hash1.getOrDefault(out,0)) count--;
                    hash2.put(out,hash2.get(out)-1);
                    left += len;
                }
                //更新
                if(count == m) ret.add(left);
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        String s = "wordgoodgoodgoodbestword";
        String[] words = {"word","good","best","word"};
        List<Integer> ret = new ArrayList<>();
        ret = findSubstring(s,words);
        System.out.println(ret);
    }
}